<?php

class ShowTable {
	private $makeLink = False;
	private $varname = "";
	function main() {
		selectDatabase();
		var_dump($result);
		$this->displayData($result);

	}
	function generateQuery() {
		//gerates a query from the exisiting session that only selects the ID in a single table.
		//could ab$link = parent::getdata("localhost", "root", "murry", "murry_LN2_database);stract more
		$query = "SELECT * FROM " . $_SESSION['table'] . " WHERE ". $_SESSION['name'] . " =  ". $_POST['generic'];		
		return $query;
	}
	function makeHeader($result, $linkArray = 0) {
		while ($offset < mysql_num_fields($result)) {  //Prints out header column names
			$meta = mysql_fetch_field($result, $offset);
			$offset++;
			if ($linkArray[$meta -> name] == "") { //if the value does not exist in link array print out the name.
				$select .= "<Th>" . $meta ->name . "</th> \n";
			}
		}
		return $select;
	}
	/*
	function makeHeader($result, $linkArray = 0) {
		while ($offset < mysql_num_fields($result)) {  //Prints out header column names
			$meta = mysql_fetch_field($result, $offset);
			$offset++;
			if ($linkArray[$meta -> name] == "") { //if the value does not exist in link array print out the name.
				$select .= "<Th>" . shiftName($meta ->name) . "</th> \n";
			}
		}
		return $select;
	}
	*/
	function displayData($result, $linkArray = 0, $extraLinks = 0)  {
		//displays data from a result in a table form.  Should return a string instead of an $result is the result of a query $linkArray is an array of keys -> values of where you want your links to go.  keys are the column in a query values are the page.  
		
		$select .=  "<table border = 1>\n";
		$select .= "\t <tr> \n";
		$select .= $this -> makeHeader($result, $linkArray);
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { //Prints out all data with links speficied.  
			$select .= "\t<tr>\n";
			$select .= $this -> makeCell($line, $linkArray, false, $extraLinks);
			$select .= "\t</tr>\n";
		}
		$select .= "</table>\n";
		return $select;
	}
	
	 function makeCell($line, $linkArray, $formatCell = False, $extraLinks = 0) {
		$loopCount = 0;
		if($formatCell) {
			$select .= "<tr bgcolor=\"CCFFCC\"><td> <font color= \"0066FF\">" .ModifyFrozenTube::printLocation($line) . "</font>  </td></tr>";
			unset($line['Location']);
			unset($line['ContainerID']);
		}
		foreach ($line as $key_value => $col_value)  {
			
			$tempSelect = $this -> makeEntry($makeLink, $linkArray, $key_value, $col_value, $oldKey_value, $oldCol_value, $varname, $extraLinks);
			if($tempselect) {//if it returns anything
				$loopCount++;
			}
			
			if($formatCell) { 
				if($loopCount % 2 == 0) {
				$tempSelect .= "</tr><tr>";
				}
			}
			$select .= $tempSelect;
			$oldCol_value = $col_value;
			$oldKey_value = $key_value;
		}
		
		return $select;
	}
	function makeEntry($makeLink, $linkArray, $key_value, $col_value, $oldKey_value, $oldCol_value, $varname = 0, $extraLinks) { 
		if ($this -> makeLink) { //Website is the website to be directed to $key_value is the variable name to $_GET in the new website $varname is the WHILE condition in the new website.
			$select .= "<td> <a href=\"" . $this -> varname . "?" . $oldKey_value . "=" .$oldCol_value ."$extraLinks\">" . $col_value . "</a> </td>"; 
			$this -> makeLink = FALSE;
		} elseif ($linkArray[$key_value] == "") {		
			
			if($col_value== 1 & $key_value== "isPrimary") {
				$select .= "<td> True </td> \n ";
			} elseif ($col_value == 0 & $key_value== "isPrimary") {
				$select .= "<td> False </td> \n";
			} else {
				if($col_value == "") {
					$col_value = "&nbsp;";
				}
				$select .= "\t\t <td>$col_value</td> \n";
			}
		} else {
			$this -> varname = $linkArray[$key_value];
			$this -> makeLink = TRUE;
		
		}
		
		return $select;
	}
	function getContainerInfo() {
		//Gets the info about a specific container speficied by the user and displayes it in list form.  Really should be its own webpage.
		selectDatabase();
		$query = "SELECT CellType.ID AS \"CellTypeID\", CellType.Name AS 'Cell Name', Modification.id AS \"ModificationID\", Modification.Name 'Modification Name', Researcher.ID AS \"ResearcherID\", Researcher.Name FROM FrozenTube JOIN CellType ON (FrozenTube.CellTypeID=CellType.ID) JOIN Modification ON (FrozenTube.ModifiedByID=Modification.ID) JOIN Researcher ON ( FrozenTube.FrozenByID=Researcher.ID) WHERE FrozenTube.ContainerID = " . $_POST['generic'];
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());

		//$this->showListView($result);
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		$this ->displayData($result, array('CellTypeID' => 'ShowAllCell.php', 'ModificationID' => 'showAllCell.php', 'ResearcherID' => 'showAllCell.php'));
	}
	

	
	function showListView($result, $linkArray = 0) {
		$boxQuery = "SELECT numRows, numCols, LetterHeight, LetterWidth FROM ContainerType JOIN Container ON (Container.ContainerTypeID=ContainerType.id) WHERE Container.ID=";
		if($_GET['ContainerID'] != "") {
			$boxQuery .= $_GET['ContainerID'];
		} else {
			$boxQuery .= $_POST['ContainerID'];
		}

		
		//$toLetter = new ModifyFrozenTube();
		//Displaies a box in the format of a box $result is the information displayed in the box format.   
		$box = mysql_query($boxQuery) or die('Query failed: ' . mysql_error());  //gets box dementions
		
		$databaseRow = mysql_fetch_array($box, MYSQL_ASSOC);
		$numRow = $databaseRow['numRows'];
		$numCol = $databaseRow['numCols']; //Nesting tables to display info in a list format 
		$select .= "<table border = 0>\n";
		$select .= "<tr>";
		$select .= "<td></td>";
		for($i = 0; $i < $row; $i++) {
			/*
			$header = $i + 1;
			if($databaseRow['LetterWidth'])
				$header = ModifyFrozenTube::convertToLetter($header);
			$select .= "<th>$header</th>";
			*/
		}
		$select .= "</tr>";
		for ($col = 0; $col< $numCol; $col++) { //
			$select .= "<tr>";
			/*
			$header = $col + 1;
			if($databaseRow['LetterHeight'])
				//$header = ModifyFrozenTube::convertToLetter($header);
			$select .= "<th>$header</th>";
   			*/
			for($row = 0; $row < $numRow; $row++) {
				$bgColor = "bgcolor = \"";
				if(($col % 2 == 0 & $row % 2 ==0) | ($col % 2 == 1 & $row % 2 == 1)) {
					$bgColor .= "EEEEFF";
				} else {
					$bgColor .= "EEEEEE"	;
				}
				$bgColor .= "\"";
   				$select .= "<td Valign=\"top\" $bgColor>";
   				
   				$line = mysql_fetch_array($result, MYSQL_ASSOC);
   				//$select .= $line['Name'];
				$select .= "<table width=\"100%\">";
				$select .= $this -> makeCell($line, $linkArray, True);
			
   				$select .="</table>";
   				$select .= "</td>";
   					
   			}
   			$select .= "</tr>";
		}
		$select .= "</table> \n";
		mysql_free_result($result);
		return $select;
	}
	
	function makeBoxFooter() {
		$query = "select id, Tank, Tower, Position FROM Container";
		selectDatabase();
		$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
		$result = mysql_query($query);
			while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$tableArray[$line['Tank']][$line['Tower']][$line['Position']] = $line['id'];
			}
			$select .="<table border= \"0\"> \n";
			
			//echo print_r($tableArray);
		foreach($tableArray as $tankName => $tank) {
			$select .= "	<tr> \n";
			$select .="	<td Valign=\"top\"> \n";
			$select .="	<table border=\"0\"><tr> \n";
			$select .= "		<tr> \n";
			$select .="		<td Valign=\"top\"> $tankName </td> \n \n";
			$select .="		</tr> \n";
			$select .="		<tr> \n";
				
			foreach ($tank as $towerName => $tower) {
				$select .= "		<td Valign=\"top\">";
				$select .= "		<table border=\"2\"> \n"; 
				$select .="			<tr> \n";
				$select .= "			<th>Tower: $towerName </th> \n";
				
					
				foreach ($tower as $position => $id) {
						
					$select .= "				<tr> \n";
					$select .=" 				<td Valign=\"top\"> <a href=\"ViewBox.php?ContainerID=$id\"> Position: $position</a> \n";
					$select .= "				</td> \n";
					$select .="				</tr> \n";
				
				
					}
					
					$select .="			</tr> \n";
					$select.= "		</table> \n";
					$select.="		</td>";
				}
				
				
				$select .= "		</tr> \n";
				$select .= "	</tr>";
				$select .="</table> \n";
				
	
			}
			
			$select .= "</table> \n";
			echo $select;
	}
	
}
?>

<HTML>
<HEAD>
<TITLE> MySQL Databases</TITLE>
</HEAD>
<BODY>


<? //echo "$select"; ?>

</BODY>
</HTML>
